<?php
	class myString {
		function cutString($str,$num){
			$arr = split(" ",$str);
			$r = "";
			for($i=0;$i<count($arr);$i++){
				if($i<=$num){
					$r .= $arr[$i] . " ";
				}
			}
			return $r;
		}
		function cutString2($str,$num){
			$arr = str_split($str);
			$r = "";
			for($i=0;$i<count($arr);$i++){
				if($i<$num){
					$r .= $arr[$i];
				}
			}
			$r .= "...";
			return $r;
		}
		function unQuote($str){
			$str2 = "";
			$str2 = ucfirst($str);
			$str2 = str_replace("\'", "'",$str2);
			$str2 = str_replace('\"', '"',$str2);
			return $str2;
		}
		
		function cheonguoc( $text )
		{
			$text = str_replace( "\\", '&#92;', $text );
			return $text;
		}
		
		
		function FixQuotes( $what = "" )
		{
		
			$what = ereg_replace( "'", "''", $what );
			while ( eregi("\\\\'", $what) )
			{
				$what = ereg_replace( "\\\\'", "'", $what );
			}
			return $what;
		}
		function nl2brStrict( $text )
{
	$text = trim( $text );
	$text = ereg_replace( " \r\n", "\r\n", $text );
	$text = str_replace( "\r", '', $text );
	$text = preg_replace( '/(?<!>)\n/', "<br />", $text );
	return $text;
}
		
		
		function VNIname($str)
		{
			return str_replace(
			Array('ỵ','ỹ','ỷ','ỳ','ý',
			'ự','ữ','ử','ừ','ứ','ư',
			'ụ','ũ','ủ','ù','ú',
			'ợ','ỡ','ở','ờ','ớ','ơ',
			'ộ','ỗ','ổ','ồ','ố','ô',
			'ọ','õ','ỏ','ò','ó',
			'ị','ĩ','ỉ','ì','í',
			'ệ','ễ','ể','ề','ế','ê',
			'ẹ','ẽ','ẻ','è','é',
			'đ',
			'ậ','ẫ','ẩ','ầ','ấ','â',
			'ặ','ẵ','ẳ','ằ','ắ','ă',
			'ạ','ã','ả','à','á',
			'Ỵ','Ỹ','Ỷ','Ỳ','Ý',
			'Ự','Ữ','Ử','Ừ','Ứ','Ư',
			'Ụ','Ũ','Ủ','Ù','Ú',
			'Ợ','Ỡ','Ở','Ờ','Ớ','Ơ',
			'Ộ','Ỗ','Ổ','Ồ','Ố','Ô',
			'Ọ','Õ','Ỏ','Ò','Ó',
			'Ị','Ĩ','Ỉ','Ì','Í',
			'Ệ','Ễ','Ể','Ề','Ế','Ê',
			'Ẹ','Ẽ','Ẻ','È','É',
			'Đ',
			'Ậ','Ẫ','Ẩ','Ầ','Ấ','Â',
			'Ặ','Ẵ','Ẳ','Ằ','Ắ','Ă',
			'Ạ','Ã','Ả','À','Á'),
		
			Array('y5','y4','y3','y2','y1',
			'u75','u74','u73','u72','u71','u70',
			'u5','u4','u3','u2','u1',
			'o75','o74','o73','o72','o71','o70',
			'o65','o64','o63','o62','o61','o60',
			'o5','o4','o3','o2','o1',
			'i5','i4','i3','i2','i1',
			'e65','e64','e63','e62','e61','e60',
			'e5','e4','e3','e2','e1',
			'd9',
			'a65','a64','a63','a62','a61','a60',
			'a85','a84','a83','a82','a81','a80',
			'a5','a4','a3','a2','a1',
			'Y5','Y4','Y3','Y2','Y1',
			'U75','U74','U73','U72','U71','U70',
			'U5','U4','U3','U2','U1',
			'O75','O74','O73','O72','O71','O70',
			'O65','O64','O63','O62','O61','O60',
			'O5','O4','O3','O2','O1',
			'I5','I4','I3','I2','I1',
			'E65','E64','E63','E62','E61','E60',
			'E5','E4','E3','E2','E1',
			'D9',
			'A65','A64','A63','A62','A61','A60',
			'A85','A84','A83','A82','A81','A80',
			'A5','A4','A3','A2','A1'),
			$str
			);
		}
		
		
		function UVNIname($str)
		{
			return  str_replace(
			Array('y5','y4','y3','y2','y1',
			'u75','u74','u73','u72','u71','u70',
			'u5','u4','u3','u2','u1',
			'o75','o74','o73','o72','o71','o70',
			'o65','o64','o63','o62','o61','o60',
			'o5','o4','o3','o2','o1',
			'i5','i4','i3','i2','i1',
			'e65','e64','e63','e62','e61','e60',
			'e5','e4','e3','e2','e1',
			'd9',
			'a65','a64','a63','a62','a61','a60',
			'a85','a84','a83','a82','a81','a80',
			'a5','a4','a3','a2','a1',
			'Y5','Y4','Y3','Y2','Y1',
			'U75','U74','U73','U72','U71','U70',
			'U5','U4','U3','U2','U1',
			'O75','O74','O73','O72','O71','O70',
			'O65','O64','O63','O62','O61','O60',
			'O5','O4','O3','O2','O1',
			'I5','I4','I3','I2','I1',
			'E65','E64','E63','E62','E61','E60',
			'E5','E4','E3','E2','E1',
			'D9',
			'A65','A64','A63','A62','A61','A60',
			'A85','A84','A83','A82','A81','A80',
			'A5','A4','A3','A2','A1'),
			
			Array('ỵ','ỹ','ỷ','ỳ','ý',
			'ự','ữ','ử','ừ','ứ','ư',
			'ụ','ũ','ủ','ù','ú',
			'ợ','ỡ','ở','ờ','ớ','ơ',
			'ộ','ỗ','ổ','ồ','ố','ô',
			'ọ','õ','ỏ','ò','ó',
			'ị','ĩ','ỉ','ì','í',
			'ệ','ễ','ể','ề','ế','ê',
			'ẹ','ẽ','ẻ','è','é',
			'đ',
			'ậ','ẫ','ẩ','ầ','ấ','â',
			'ặ','ẵ','ẳ','ằ','ắ','ă',
			'ạ','ã','ả','à','á',
			'Ỵ','Ỹ','Ỷ','Ỳ','Ý',
			'Ự','Ữ','Ử','Ừ','Ứ','Ư',
			'Ụ','Ũ','Ủ','Ù','Ú',
			'Ợ','Ỡ','Ở','Ờ','Ớ','Ơ',
			'Ộ','Ỗ','Ổ','Ồ','Ố','Ô',
			'Ọ','Õ','Ỏ','Ò','Ó',
			'Ị','Ĩ','Ỉ','Ì','Í',
			'Ệ','Ễ','Ể','Ề','Ế','Ê',
			'Ẹ','Ẽ','Ẻ','È','É',
			'Đ',
			'Ậ','Ẫ','Ẩ','Ầ','Ấ','Â',
			'Ặ','Ẵ','Ẳ','Ằ','Ắ','Ă',
			'Ạ','Ã','Ả','À','Á'),
			$str
			);
		
		}
		
		// Chuyen doi tieng viet co dau sang khong dau
      function vietdecode($value){
               #---------------------------------a
               $value = str_replace("á", "a", $value);
               $value = str_replace("à", "a", $value);
               $value = str_replace("ả", "a", $value);
               $value = str_replace("ã", "a", $value);
               $value = str_replace("ạ", "a", $value);
               $value = str_replace("â", "a", $value);
               $value = str_replace("ă", "a", $value);
               #---------------------------------A
               $value = str_replace("Á", "a", $value);
               $value = str_replace("À", "a", $value);
               $value = str_replace("Ả", "a", $value);
               $value = str_replace("Ã", "a", $value);
               $value = str_replace("Ạ", "a", $value);
               $value = str_replace("Â", "a", $value);
               $value = str_replace("Ă", "a", $value);
               #---------------------------------a^
               $value = str_replace("ấ", "a", $value);
               $value = str_replace("ầ", "a", $value);
               $value = str_replace("ẩ", "a", $value);
               $value = str_replace("ẫ", "a", $value);
               $value = str_replace("ậ", "a", $value);
               #---------------------------------A^
               $value = str_replace("Ấ", "a", $value);
               $value = str_replace("Ầ", "a", $value);
               $value = str_replace("Ẩ", "a", $value);
               $value = str_replace("Ẫ", "a", $value);
               $value = str_replace("Ậ", "a", $value);
               #---------------------------------a(
               $value = str_replace("ắ", "a", $value);
               $value = str_replace("ằ", "a", $value);
               $value = str_replace("ẳ", "a", $value);
               $value = str_replace("ẵ", "a", $value);
               $value = str_replace("ặ", "a", $value);
               #---------------------------------A(
               $value = str_replace("Ắ", "a", $value);
               $value = str_replace("Ằ", "a", $value);
               $value = str_replace("Ẳ", "a", $value);
               $value = str_replace("Ẵ", "a", $value);
               $value = str_replace("Ặ", "a", $value);
               #---------------------------------e
               $value = str_replace("é", "e", $value);
               $value = str_replace("è", "e", $value);
               $value = str_replace("ẻ", "e", $value);
               $value = str_replace("ẽ", "e", $value);
               $value = str_replace("ẹ", "e", $value);
               $value = str_replace("ê", "e", $value);
               #---------------------------------E
               $value = str_replace("É", "e", $value);
               $value = str_replace("È", "e", $value);
               $value = str_replace("Ẻ", "e", $value);
               $value = str_replace("Ẽ", "e", $value);
               $value = str_replace("Ẹ", "e", $value);
               $value = str_replace("Ê", "e", $value);
               #---------------------------------e^
               $value = str_replace("ế", "e", $value);
               $value = str_replace("ề", "e", $value);
               $value = str_replace("ể", "e", $value);
               $value = str_replace("ễ", "e", $value);
               $value = str_replace("ệ", "e", $value);
               #---------------------------------E^
               $value = str_replace("Ế", "e", $value);
               $value = str_replace("Ề", "e", $value);
               $value = str_replace("Ể", "e", $value);
               $value = str_replace("Ễ", "e", $value);
               $value = str_replace("Ệ", "e", $value);
               #---------------------------------i
               $value = str_replace("í", "i", $value);
               $value = str_replace("ì", "i", $value);
               $value = str_replace("ỉ", "i", $value);
               $value = str_replace("ĩ", "i", $value);
               $value = str_replace("ị", "i", $value);
               #---------------------------------I
               $value = str_replace("Í", "i", $value);
               $value = str_replace("Ì", "i", $value);
               $value = str_replace("Ỉ", "i", $value);
               $value = str_replace("Ĩ", "i", $value);
               $value = str_replace("Ị", "i", $value);
               #---------------------------------o^
               $value = str_replace("ố", "o", $value);
               $value = str_replace("ồ", "o", $value);
               $value = str_replace("ổ", "o", $value);
               $value = str_replace("ỗ", "o", $value);
               $value = str_replace("ộ", "o", $value);
               #---------------------------------O^
               $value = str_replace("Ố", "o", $value);
               $value = str_replace("Ồ", "o", $value);
               $value = str_replace("Ổ", "o", $value);
               $value = str_replace("Ô", "o", $value);
               $value = str_replace("Ộ", "o", $value);
               #---------------------------------o*
               $value = str_replace("ớ", "o", $value);
               $value = str_replace("ờ", "o", $value);
               $value = str_replace("ở", "o", $value);
               $value = str_replace("ỡ", "o", $value);
               $value = str_replace("ợ", "o", $value);
               #---------------------------------O*
               $value = str_replace("Ớ", "o", $value);
               $value = str_replace("Ờ", "o", $value);
               $value = str_replace("Ở", "o", $value);
               $value = str_replace("Ỡ", "o", $value);
               $value = str_replace("Ợ", "o", $value);
               #---------------------------------u*
               $value = str_replace("ứ", "u", $value);
               $value = str_replace("ừ", "u", $value);
               $value = str_replace("ử", "u", $value);
               $value = str_replace("ữ", "u", $value);
               $value = str_replace("ự", "u", $value);
               #---------------------------------U*
               $value = str_replace("Ứ", "u", $value);
               $value = str_replace("Ừ", "u", $value);
               $value = str_replace("Ử", "u", $value);
               $value = str_replace("Ữ", "u", $value);
               $value = str_replace("Ự", "u", $value);
               #---------------------------------y
               $value = str_replace("ý", "y", $value);
               $value = str_replace("ỳ", "y", $value);
               $value = str_replace("ỷ", "y", $value);
               $value = str_replace("ỹ", "y", $value);
               $value = str_replace("ỵ", "y", $value);
               #---------------------------------Y
               $value = str_replace("Ý", "y", $value);
               $value = str_replace("Ỳ", "y", $value);
               $value = str_replace("Ỷ", "y", $value);
               $value = str_replace("Ỹ", "y", $value);
               $value = str_replace("Ỵ", "y", $value);
               #---------------------------------DD
               $value = str_replace("Đ", "d", $value);
               $value = str_replace("Đ", "d", $value);
               $value = str_replace("đ", "d", $value);
               #---------------------------------o
               $value = str_replace("ó", "o", $value);
               $value = str_replace("ò", "o", $value);
               $value = str_replace("ỏ", "o", $value);
               $value = str_replace("õ", "o", $value);
               $value = str_replace("ọ", "o", $value);
               $value = str_replace("ô", "o", $value);
               $value = str_replace("ơ", "o", $value);
               #---------------------------------O
               $value = str_replace("Ó", "o", $value);
               $value = str_replace("Ò", "o", $value);
               $value = str_replace("Ỏ", "o", $value);
               $value = str_replace("Õ", "o", $value);
               $value = str_replace("Ọ", "o", $value);
               $value = str_replace("Ô", "o", $value);
               $value = str_replace("Ơ", "o", $value);
               #---------------------------------u
               $value = str_replace("ú", "u", $value);
               $value = str_replace("ù", "u", $value);
               $value = str_replace("ủ", "u", $value);
               $value = str_replace("ũ", "u", $value);
               $value = str_replace("ụ", "u", $value);
               $value = str_replace("ư", "u", $value);
               #---------------------------------U
               $value = str_replace("Ú", "u", $value);
               $value = str_replace("Ù", "u", $value);
               $value = str_replace("Ủ", "u", $value);
               $value = str_replace("Ũ", "u", $value);
               $value = str_replace("Ụ", "u", $value);
               $value = str_replace("Ư", "u", $value);
               #---------------------------------
               $value = str_replace("."," ", $value);
               $value = str_replace(","," ", $value);
               $value = str_replace("!"," ", $value);
               $value = str_replace("?"," ", $value);
               $value = str_replace(":", " ", $value);
               $value = str_replace("'", " ", $value);
               $value = str_replace("&#039;", " ", $value);
               $value = str_replace("&quot;", " ", $value);
               $value = str_replace("&amp;","va", $value);
               $value = str_replace("(", " ", $value);
               $value = str_replace(")", " ", $value);
               $value = str_replace("-", " ", $value);
               $value = str_replace("   ", " ", $value);
               $value = str_replace("  ", " ", $value);
               return strtolower(str_replace(" ","-",trim($value)));
      }
	  
	  function HTML($t=""){
               //$t = addslashes($t);
               $text = nl2br($t);
               $text = str_replace("[url]http://","[url]",$text);
               $text = str_replace("[url=http://","[url=",$text);
               //$text = preg_replace("/(http.*:\/\/.+)\s/U", "<a href=\"$1\">$1</a> ", $text);
               $text = preg_replace('/(\[b\])(.+?)(\[\/b\])/', '<b>\\2</b>',$text);
               $text = preg_replace('/(\[i\])(.+?)(\[\/i\])/', '<i>\\2</i>',$text);
               $text = preg_replace('/(\[u\])(.+?)(\[\/u\])/', "<u>\\2</u>", $text);
               $text = preg_replace('/(\[color=(.+?)\])(.+?)(\[\/color\])/', '<font color=\\2>\\3</font>',$text);
               $text = preg_replace('/(\[email\])(.+?)(\[\/email\])/', "<a href=\"mailto:\\2\">\\2 </a>", $text);
               $text = preg_replace('/(\[email=(.+?)\])(.+?)(\[\/email\])/', "<a href=\"mailto:\\2\">\\3</a>", $text);
               $text = preg_replace('/(\[url\])(.+?)(\[\/url\])/', "<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);
               $text = preg_replace('/(\[url=\])(.+?)(\[\/url\])/', "<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);
               $text = preg_replace('/(\[url=(.+?)\])(.+?)(\[\/url\])/', "<a href=\"http://\\2\" target=\"_blank\">\\3</a>", $text);
               $text = stripslashes($text);
               $text = str_replace("!!!!", "!", $text);
               $text = str_replace( "[img]", "<img align=right width=150 src=news_images/"  , $text );
               $text = str_replace( "[/img]", " />"  , $text );
               return $text;
      }

      function txt_HTML($t=""){
               //$t = addslashes($t);
               $t = preg_replace("/&(?!#[0-9]+;)/s", '&amp;', $t );
               $t = str_replace( "<", "&lt;"  , $t );
               $t = str_replace( ">", "&gt;"  , $t );
               $t = str_replace( '"', "&quot;", $t );
               $t = str_replace( "'", '&#039;', $t );
               return $t;
      }

      function txt_unHTML($t=""){
               $t = stripslashes($t);
               //$t = nl2br($t);
               $t = preg_replace("/&(?!#[0-9]+;)/s", '&amp;', $t );
               $t = str_replace( "<", "&lt;"  , $t );
               $t = str_replace( ">", "&gt;"  , $t );
               $t = str_replace( '"', "&quot;", $t );
               $t = str_replace( "'", '&#039;', $t );
               return $t;
      }

      function makedate($text) {
               $tmp = explode ("-",$text);
               return $tmp[2]."/".$tmp[1]."/".$tmp[0];
      }
		
		function BIGstr($data)
		{
			$data=str_replace("Q","q",$data);
			$data=str_replace("W","w",$data);
			$data=str_replace("E","e",$data);
			$data=str_replace("R","r",$data);
			$data=str_replace("T","t",$data);
			$data=str_replace("Y","y",$data);
			$data=str_replace("U","u",$data);
			$data=str_replace("I","i",$data);
			$data=str_replace("O","o",$data);
			$data=str_replace("P","p",$data);
			$data=str_replace("A","a",$data);
			$data=str_replace("S","s",$data);
			$data=str_replace("D","d",$data);
			$data=str_replace("F","f",$data);
			$data=str_replace("G","g",$data);
			$data=str_replace("H","h",$data);
			$data=str_replace("J","j",$data);
			$data=str_replace("K","k",$data);
			$data=str_replace("L","l",$data);
			$data=str_replace("Z","z",$data);
			$data=str_replace("X","x",$data);
			$data=str_replace("C","c",$data);
			$data=str_replace("V","v",$data);
			$data=str_replace("B","b",$data);
			$data=str_replace("N","n",$data);
			$data=str_replace("M","m",$data);
			return $data;
		}
		
		function smallSTR($data)
		{
		
			$data=str_replace("q","Q",$data);
			$data=str_replace("w","W",$data);
			$data=str_replace("e","E",$data);
			$data=str_replace("r","R",$data);
			$data=str_replace("t","T",$data);
			$data=str_replace("y","Y",$data);
			$data=str_replace("u","U",$data);
			$data=str_replace("i","I",$data);
			$data=str_replace("o","O",$data);
			$data=str_replace("p","P",$data);
			$data=str_replace("a","A",$data);
			$data=str_replace("s","S",$data);
			$data=str_replace("d","D",$data);
			$data=str_replace("f","F",$data);
			$data=str_replace("g","G",$data);
			$data=str_replace("h","H",$data);
			$data=str_replace("j","J",$data);
			$data=str_replace("k","K",$data);
			$data=str_replace("l","L",$data);
			$data=str_replace("z","Z",$data);
			$data=str_replace("x","X",$data);
			$data=str_replace("c","C",$data);
			$data=str_replace("v","V",$data);
			$data=str_replace("b","B",$data);
			$data=str_replace("n","N",$data);
			$data=str_replace("m","M",$data);
			return $data;
		}
		
		function UNIstr($str)
		{
			$str=VNIname($str);
			$strR=explode(" ",$str);
			$strINFO="";
			for($i=0; $i<count($strR); $i++) {
			for($j=0; $j<strlen($strR[$i]); $j++) 
			{
			if($j==0)$strINFO.=" ".smallSTR($strR[$i][$j]); 
			else $strINFO.="".BIGstr($strR[$i][$j]);
			}
			}
			return trim(UVNIname($strINFO));
		}
		
		
		      function clean_key($key) {
                if ($key == ""){
                    return "";
                }
                $key = preg_replace( "/\.\./"           , ""  , $key );
                $key = preg_replace( "/\_\_(.+?)\_\_/"  , ""  , $key );
                $key = preg_replace( "/^([\w\.\-\_]+)$/", "$1", $key );
                return $key;
       }

       function clean_value($val) {
                if ($val == ""){
                    return "";
                }
                $val = str_replace( "&#032;", " ", $val );
                $val = str_replace( "&"            , "&amp;"         , $val );
                $val = str_replace( "<!--"         , "&#60;&#33;--"  , $val );
                $val = str_replace( "-->"          , "--&#62;"       , $val );
                $val = preg_replace( "/<script/i"  , "&#60;script"   , $val );
                $val = str_replace( ">"            , "&gt;"          , $val );
                $val = str_replace( "<"            , "&lt;"          , $val );
                $val = str_replace( "\""           , "&quot;"        , $val );
                $val = preg_replace( "/\n/"        , "<br />"        , $val ); // Convert literal newlines
                $val = preg_replace( "/\\\$/"      , "&#036;"        , $val );
                $val = preg_replace( "/\r/"        , ""              , $val ); // Remove literal carriage returns
                $val = str_replace( "!"            , "&#33;"         , $val );
                $val = str_replace( "'"            , "&#39;"         , $val ); // IMPORTANT: It helps to increase sql query safety.
                // Ensure unicode chars are OK
                $val = preg_replace("/&amp;#([0-9]+);/s", "&#\\1;", $val );
                // Swop user inputted backslashes
                // $val = preg_replace( "/\\\(?!&amp;#|\?#)/", "&#092;", $val );
                return $val;
       }
	}

?>